package com.example.primarycalculator2;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearSmoothScroller;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

public class HomeworkActivity extends AppCompatActivity implements View.OnClickListener {
    public ArrayList<Problem> problems=new ArrayList<>();
    ListView listView;
    private ProblemAdapter adapter;
    private TextView textView;
    private int recInt=0;
    Handler handler=new Handler();
    Runnable runnable=new Runnable() {
        @Override
        public void run() {
            if(recInt!=0){
                int min=recInt/60;
                int second=recInt%60;
                String Min="";
                String Second="";
                if (second<10){
                    Second="0"+second;
                }else {
                    Second+=""+second;
                }
                if(min<10){
                    Min="0"+min;
                }else {
                    Min+=""+min;
                }
                textView.setText(Min+":"+Second);
                recInt--;
                handler.postDelayed(this,1000);
            }else {
                textView.setText("00:00");
                Intent intent = new Intent(HomeworkActivity.this, AnswerActivity.class);
                intent.putParcelableArrayListExtra("list", (ArrayList<? extends Parcelable>) problems);
                intent.putExtra("lefttime",recInt);
                startActivity(intent);
            }

        }
    };




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homework);

        Intent intent=getIntent();
        int minute=intent.getIntExtra("minutes",3);
        int num=intent.getIntExtra("num_problems",10);
//        Log.d("HomeworkActivity : 时间", String.valueOf(minute));

//        记录一共做了多少题目
        SharedPreferences sharedPreferences=getSharedPreferences("menta",Activity.MODE_PRIVATE);
        int numAall=sharedPreferences.getInt("num",0);
        numAall+=num;
        SharedPreferences.Editor editor= sharedPreferences.edit();
        editor.putInt("num",numAall);
        editor.apply();



        initProblem(num);
        adapter=new ProblemAdapter(HomeworkActivity.this,R.layout.problem_item, problems);
        listView=findViewById(R.id.list_view);
        listView.setAdapter(adapter);


        textView=findViewById(R.id.time);
        recInt=minute*60;
        handler.postDelayed(runnable,1000);


    }

    private  void searchPro(ArrayList<Problem> pros){
        for (Problem problem:pros){
            Log.d("Pro:problem",problem.getContent());
            Log.d("Pro:problem",problem.getAnswer());
            Log.d("Pro:problem",problem.getRealAnswer());

        }
    }

    private void initProblem(int num){
        ProblemsGenerator pg= new ProblemsGenerator();
        problems=pg.create(num);
    }



    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.sub_pro){
            Intent intent = new Intent(HomeworkActivity.this, AnswerActivity.class);
            intent.putParcelableArrayListExtra("list", (ArrayList<? extends Parcelable>) problems);
            intent.putExtra("lefttime",recInt);

            handler.removeCallbacksAndMessages(null);

            startActivity(intent);

        }
    }






}